package cn.glinks.liuhe.api;

import cn.glinks.liuhe.api.jsonbean.PersonalInfo;
import cn.glinks.liuhe.api.jsonbean.PersonalInterest;
import cn.glinks.liuhe.api.jsonbean.RegisterInfo;
import cn.glinks.liuhe.baserx.BaseResponse;
import cn.glinks.liuhe.baserx.RxSchedulers;
import cn.glinks.liuhe.model.Question;
import cn.glinks.liuhe.utils.GsonUtils;
import java.util.HashMap;
import java.util.List;
import okhttp3.RequestBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import rx.Observable;
import rx.functions.Func1;

/**
 * Des:API请求接口
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-22 14:17
 */
public class ApiRequest {

  /**
   * 获取短信验证码
   *
   * @param phone 手机号码
   * @param action 1-用于注册  2-用于修改密码
   */
  public static Observable<String> requestGetSmscode(String phone, int action) {
    // 发起请求
    return Api.getDefault().getSmscode(phone, action)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 用户注册
   *
   * @param info RegisterInfo
   */
  public static Observable<String> requestRegister(RegisterInfo info) {
    // 构建RequestBody(Gson方式)
    String strEntity = GsonUtils.toJson(info);
    RequestBody body = RequestBody
        .create(okhttp3.MediaType.parse("application/json; charset=utf-8"), strEntity);
    // 发起请求
    return Api.getDefault().postUserRegister(body)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 用户登录
   */
  public static Observable<String> requestLogin(String phone,
      String passwd) {
    // 构建RequestBody(HashMap方式)
    HashMap<String, String> paramsMap = new HashMap<>();
    paramsMap.put("phoneNo", phone);
    paramsMap.put("password", passwd);
    String strEntity = GsonUtils.toJson(paramsMap);
    RequestBody body = RequestBody
        .create(okhttp3.MediaType.parse("application/json; charset=utf-8"), strEntity);
    // 发起请求
    return Api.getDefault().postUserLogin(body)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }


  /**
   * 忘记密码
   */
  public static Observable<String> requestResetPasswd(String phone,
      String smscode,
      String newPasswd) {
    // 构建RequestBody(HashMap方式)
    HashMap<String, String> paramsMap = new HashMap<>();
    paramsMap.put("phoneNo", phone);
    paramsMap.put("smscode", smscode);
    paramsMap.put("newPassword", newPasswd);
    String strEntity = GsonUtils.toJson(paramsMap);
    RequestBody body = RequestBody
        .create(okhttp3.MediaType.parse("application/json; charset=utf-8"), strEntity);
    // 发起请求
    return Api.getDefault().postUserForgetPasswd(body)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取测试时间状态
   */
  public static Observable<String> requestGetTestTimeStatus(String auth,
      String testTaskId,
      String beiginTime,
      String endTime) {
    // 发起请求
    return Api.getDefault().getTestTimeStatus("Bearer " + auth, testTaskId, beiginTime, endTime)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取试题
   */
  public static Observable<String> requestGetQuestions(String auth,
      String testTaskId,
      String beiginTime,
      String endTime) {
    // 发起请求
    return Api.getDefault().getQuestions("Bearer " + auth, testTaskId, beiginTime, endTime)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 提交答案
   */
  public static Observable<String> requestCommitAnswer(String auth,
      String testTaskId,
      int timeUsed,
      List<Question> list) {
    // 构建RequestBody(直接方式)
    String strEntity = "[]";
    try {
      JSONObject json = new JSONObject();
      JSONArray jsonQuestions = new JSONArray();
      // 构建答案
      for (int i = 0; i < list.size(); i++) {
        // 如果出现答案处于默认值(即未选择状态则break)
        if (list.get(i).answer == Question.NONE) {
          break;
        }
        JSONArray jsonQuestion = new JSONArray();
        jsonQuestion.put(list.get(i).index);
        jsonQuestion.put(list.get(i).answer);
        jsonQuestions.put(jsonQuestion);
      }
      // 打包全部数据
      json.put("testTaskId", testTaskId);
      json.put("timeUsed", timeUsed);
      json.put("questions", jsonQuestions);
      // 输出为字符串
      strEntity = json.toString();
    } catch (JSONException e) {
      e.printStackTrace();
    }
    // 构建RequestBody
    RequestBody body = RequestBody
        .create(okhttp3.MediaType.parse("application/json; charset=utf-8"), strEntity);
    // 发起请求
    return Api.getDefault().postAnswers("Bearer " + auth, body)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 提交个人资料
   */
  public static Observable<String> requestCommitInfo(String auth,
      PersonalInfo info) {
    // 构建RequestBody(Gson方式)
    String strEntity = GsonUtils.toJson(info);
    RequestBody body = RequestBody
        .create(okhttp3.MediaType.parse("application/json; charset=utf-8"), strEntity);
    // 发起请求
    return Api.getDefault().postPersonalInfo("Bearer " + auth, body)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 提交个人爱好
   */
  public static Observable<String> requestCommitInterest(String auth,
      PersonalInterest interest) {

    // 构建RequestBody(Gson方式)
    String strEntity = GsonUtils.toJson(interest);
    RequestBody body = RequestBody
        .create(okhttp3.MediaType.parse("application/json; charset=utf-8"), strEntity);
    // 发起请求
    return Api.getDefault().postPersonalInterest("Bearer " + auth, body)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取测评历史记录
   *
   * @param auth 用户验证
   */
  public static Observable<String> requestGetCommentHistory(String auth) {
    // 发起请求
    return Api.getDefault().getCommentHistory("Bearer " + auth)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取整体测评报告历史记录
   *
   * @param auth 用户验证
   */
  public static Observable<String> requestGetGroupCommentHistory(String auth) {
    // 发起请求
    return Api.getDefault().getGroupCommentHistory("Bearer " + auth)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取统计数据
   *
   * @param auth 用户验证
   */
  public static Observable<String> requestGetStatisticData(String auth) {
    // 发起请求
    return Api.getDefault().getStatisticsData("Bearer " + auth)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取园区建议
   *
   * @param auth 用户验证
   * @param parkCode 园区名称
   */
  public static Observable<String> requestGetParkAdvice(String auth, String parkCode) {
    // 发起请求
    return Api.getDefault().getParkAdvice("Bearer " + auth, parkCode)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取搜索结果
   *
   * @param auth 用户验证
   * @param parkCode 园区名称
   * @param name 搜索名称
   */
  public static Observable<String> requestGetSearchName(String auth, String parkCode, String name) {
    // 发起请求
    return Api.getDefault().getSearchName("Bearer " + auth, parkCode, name)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取搜索的员工信息
   *
   * @param auth 用户验证
   * @param emid 员工工号
   */
  public static Observable<String> requestGetSearchPersonInfo(String auth, String emid) {
    // 发起请求
    return Api.getDefault().getSearchPersonInfo("Bearer " + auth, emid)
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

  /**
   * 获取公司列表
   */
  public static Observable<String> requestGetCompanyList() {
    // 发起请求
    return Api.getDefault().getCompanyList()
        .map(new Func1<BaseResponse<String>, String>() {
          @Override
          public String call(BaseResponse<String> response) {
            return response.msg;
          }
        }).compose(RxSchedulers.<String>io_main());//声明线程调度
  }

}
